package com.hb.trans.mvp.presenter

import com.basiclib.base.BasePresenter
import com.basiclib.lifecycle.RxLifecycleUtils
import com.basiclib.network.RetryWithDelay
import com.basiclib.network.RxErrorHandler
import com.hb.trans.bean.Base
import com.hb.trans.bean.Period
import com.hb.trans.global.ErrorHandleSubscriber
import com.hb.trans.mvp.contract.TransRecordContract
import com.hb.trans.mvp.model.TransRecordModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject

class TransRecordPresenter @Inject constructor(model: TransRecordModel, var errorHandler: RxErrorHandler) :
    BasePresenter<TransRecordContract.View, TransRecordContract.Model>(model) {

    fun getPeriodDetail(stunum: String, subject: Int) {
        mModel.getPeriodDetail(stunum, subject)
            .subscribeOn(Schedulers.io())
            .retryWhen(RetryWithDelay())
            .observeOn(AndroidSchedulers.mainThread())
            .compose(RxLifecycleUtils.bindToLifecycle(mRootView!!))
            .subscribe(object : ErrorHandleSubscriber<Base<MutableList<Period>>>(errorHandler) {
                override fun next(r: Base<MutableList<Period>>) {
                    if (r.code == 200) {
                        mRootView?.setupData(r.data)
                    } else {
                        mRootView?.showShortToast(r.info ?: "")
                    }
                }
            })
    }
}